new Vue({
    el: "#app",
    data: {
        userList: [],
        user: {},
        ids: []
    },
    methods: {
        //查询所有
        findAll() {
            //alert("呵呵")
            axios.get("user", {
                params: {
                    method: "findAll"
                }

            })
                .then(res => {
                    this.userList = res.data;
                })
        },
        //打开保存页面
        openSave() {
            this.user = {};
            $("#myModal1").modal("show");
        },
        //保存用户
        save() {
            axios.post("user?method=save", this.user)
                .then(res => {
                    alert(res.data)
                    if ("1" == res.data) {
                        this.findAll();
                        //成功添加
                        alert("成功添加")
                    } else {
                        //添加失败
                        alert("添加失败")
                    }
                })
        },
        //用户信息回显
        findById(id) {
            //alert(id)
            $("#myModal").modal("show");
            this.user = {};
            axios.get("user", {
                params: {
                    method: "findById",
                    uid: id
                }
            })
                .then(res => {
                    //alert(res.data.username)
                    //将后台返回的数据赋值给user
                    this.user = res.data;
                })
        },


        //修改用户信息
        update() {
            //alert(this.user.id)
            axios.post("user?method=update", this.user)
                .then(res => {
                    alert("修改成功");
                    this.findAll();
                })


        },
        deleteByIds() {
            if (this.ids.length > 0) {
                if (confirm("憨批宁确定要删除吗")) {
                    //alert(this.ids);
                    axios.post("user?method=deleteById", this.ids)
                        .then(res => {

                            this.findAll();
                            alert("删除成功");
                        })

                }
            }

        }


    },
    //钩子函数,页面打开显示数据
    created() {
        this.findAll();
    }

});